package com.scoresystem.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.scoresystem.common.CommonValue;
import com.scoresystem.dao.ICourseDao;
import com.scoresystem.model.Course;

public class CourseDaoImpl extends BaseDaoImpl implements ICourseDao {

	public CourseDaoImpl(Connection conn) {
		super(conn);
		this.conn=conn;
	}

	@Override
	public boolean add(Course course) {
		String sql="insert into "+CommonValue.TAB_COURSE+" (teanum,coursename) "
				+" values "+"("
				+"'"+course.getTeanum()+"'"+","
				+"'"+course.getCoursename()+"'"+")";
		try{
			this.templateUpdate(sql);
			return true;
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public List<String> getCoursebyTea(String worknum) {
		String sql="select distinct coursename from "+CommonValue.TAB_COURSE+
				" where teanum="+"'"+worknum+"'";
		List<String> list=new ArrayList<String>();
		try{
			ResultSet rs=this.templateQuery(sql);
			while(rs.next()){
				String name=rs.getString(1);
				list.add(name);
			}
			return list;
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<String> getCourses() {
		String sql="select distinct coursename from "+CommonValue.TAB_COURSE;
		List<String> list=new ArrayList<String>();
		try{
			ResultSet rs=this.templateQuery(sql);
			while(rs.next()){
				String name=rs.getString(1);
				list.add(name);
			}
			return list;
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<String> getCoursesbyTea(String teanum) {
		String sql="select distinct coursename from "+CommonValue.TAB_COURSE
				+" where teanum="+"'"+teanum+"'";
		List<String> list=new ArrayList<String>();
		try{
			ResultSet rs=this.templateQuery(sql);
			while(rs.next()){
				String name=rs.getString(1);
				list.add(name);
			}
			return list;
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}

}
